/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of GW-Android.

GW-Android is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

GW-Android is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with GW-Android. If not, see <http://www.gnu.org/licenses/>. */
package br.ufes.cwtools.gw.android;

import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class AddServerActivity extends Activity {
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_server);
		setTitle("Adicionar servidor de aplicação");
	}
	
	public void confirmOnClick(View view) {
		String name = ((EditText) findViewById(R.id.server_name)).getText().toString().trim();
		String url = ((EditText) findViewById(R.id.server_url)).getText().toString().trim();
		
		if (name.length() != 0 && url.length() != 0) {
			if (!url.startsWith("http://")){
				url = "http://" + url;
			}
			try {
				new URL(url);
			} catch (MalformedURLException e) {
				Toast.makeText(this, "Formato da URL inválido. Verifique a URL e tente novamente.", Toast.LENGTH_LONG).show();
				return;
			}

			ServersDbAdapter adapter = new ServersDbAdapter(this);
			adapter.open();
			adapter.createServer(name, url);
			adapter.close();
			Toast.makeText(this, "Servidor adicionado à lista.", Toast.LENGTH_LONG).show();
			setResult(RESULT_OK);
			finish();
		} else {
			Toast.makeText(this, "Entre com um nome para o servidor e sua URL.", Toast.LENGTH_SHORT).show();
		}
	}
	
	public void cancelOnClick(View view) {
		finish();
	}
}
